﻿//
//
/////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using DataAccess;
using PhysicalEquation.Models;

/////////////////////////////////////////////////////////////////////////////
namespace PhysicalEquation.Controllers
{
    /////////////////////////////////////////////////////////////////////////
    [Authorize]
    public class EquationController : Controller
    {
        /////////////////////////////////////////////////////////////////////
        public ActionResult Index(int subjectId)
        {
            Subject subject = DbAccess.Instance.Entities.Subjects.FirstOrDefault(subj => subj.Id == subjectId);
            if (subject != null)
            {
                EquationModel model = new EquationModel { Items = new List<EquationItemModel>(), SubjectId = subjectId, SubjectName = subject.Name };
                try
                {
                    List<Equation> equations = DbAccess.Instance.Entities.Equations.Where(equation => equation.Subject_Id == subjectId && equation.Visible).ToList();
                    if (equations.Count > 0)
                    {
                        model.Items = equations.Select(equation => new EquationItemModel()
                        {
                            Id = equation.Id,
                            Author = equation.User.Name,
                            Comments = equation.Comments,
                            Date = equation.Date,
                            IsSolved = equation.IsSolved,
                            Name = equation.Name,
                            Number = equation.Number,
                            SubjectId = equation.Subject_Id
                        }).ToList();
                    }
                }
                catch (Exception ex)
                {
                    throw;
                }
                return View(model);
            }
            return null;
        }

        /////////////////////////////////////////////////////////////////////
        public PartialViewResult AddEquation(string number, string name, string comments, int subjectId)
        {
            if (ModelState.IsValid)
            {
                Equation equation = new Equation()
                {
                    Name = name,
                    Number = number,
                    Comments = comments,
                    IsSolved = false,
                    Subject_Id = subjectId,
                    Date = DateTime.Now
                };
                equation.UserReference.Value = (User)Session["UserInfo"];

                DbAccess.Instance.Entities.Equations.AddObject(equation);
                DbAccess.Instance.Entities.SaveChanges();

                ModelState.Clear();
                return PartialView("AddEquationRowView", new EquationItemModel()
                {
                    Id = equation.Id,
                    Number = equation.Number,
                    Name = equation.Name,
                    Comments = equation.Comments,
                    Author = equation.User.Name,
                    Date = equation.Date,
                    IsSolved = false,
                    SubjectId = equation.Subject_Id
                });
            }
            return null;
        }

        /////////////////////////////////////////////////////////////////////
        public ActionResult Detail(int equationId)
        {
            if (equationId > 0)
            {
                Equation equation = DbAccess.Instance.Entities.Equations.FirstOrDefault(equation1 => equation1.Id == equationId);
                IEnumerable<EquationDetail> details = DbAccess.Instance.Entities.EquationDetails;
                if (equation != null)
                {
                    EquationDetailModel res = new EquationDetailModel()
                                                  {
                                                      EquationItem = new EquationItemModel()
                                                                  {
                                                                      Author = equation.User.Name,
                                                                      Comments = equation.Comments,
                                                                      Date = equation.Date,
                                                                      Id = equation.Id,
                                                                      IsSolved = equation.IsSolved,
                                                                      Name = equation.Name,
                                                                      Number = equation.Number,
                                                                      SubjectId = equation.Subject_Id,
                                                                      SubjectName = equation.Subject.Name
                                                                  },
                                                      Answers = details.Where(eqItem => eqItem.Equation_Id == equationId).Select(item => new EquationItemDetailModel()
                                                                                           {
                                                                                               Answer = item.Answer,
                                                                                               Date = item.Date,
                                                                                               EquationId = item.Equation_Id,
                                                                                               Points = item.Points,
                                                                                               UserName = item.User.Name,
                                                                                           })
                                                  };

                    return View(res);
                }
            }
            return null;
        }

        /////////////////////////////////////////////////////////////////////
        public PartialViewResult AddEquationDetail(int equationId, string answer)
        {
            if (equationId > 0 && !String.IsNullOrEmpty(answer))
            {
                EquationDetail equationDetail = new EquationDetail()
                                                    {
                                                        Answer = answer,
                                                        Date = DateTime.Now,
                                                        Equation_Id = equationId,
                                                        Points = 0
                                                    };
                equationDetail.UserReference.Value = (User) Session["UserInfo"];
                DbAccess.Instance.Entities.EquationDetails.AddObject(equationDetail);
                DbAccess.Instance.Entities.SaveChanges();
                ModelState.Clear();
                return PartialView("AddEquationDetailRowView", new EquationItemDetailModel()
                {
                    Answer = equationDetail.Answer,
                    Date = equationDetail.Date,
                    EquationId = equationDetail.Equation_Id,
                    Points = equationDetail.Points,
                    UserName = equationDetail.User.Name
                });
            }
            return null;
        }
    }
    ///////////////////////////////////////////////////////////////////////////// 
}//PhysicalEquation.Controllers                                                              